package com.axzhengxin.platform.sync.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.axzhengxin.platform.sync.ISyncDataService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * SysUserSyncServiceImpl
 * 同步用户
 *
 * @author jiangnan
 * @date 2021/3/25
 */
@Slf4j
@Service("syncUser")
public class SysUserSyncServiceImpl implements ISyncDataService {

    private static final String DEFAULT_PLAIN_PASSWORD = "888888";

    @Autowired
    private SysUserMapper sysUserMapper;

    @SneakyThrows
    @Override
    public void exec() {
        exec2();
    }

    @SneakyThrows
    private void exec1() {
        String profile = SpringUtils.getActiveProfile();
        List<SysUser> sourceUserList = Db.use(DSFactory.get(profile))
                .find(Entity.create("sys_user").set("status", "0").set("del_flag", "0"))
                .stream()
                .filter(entity -> !"CCSC".equalsIgnoreCase(entity.getStr("LOGIN_NAME")))
                .map(entity -> {
                    SysUser user = entity.toBeanIgnoreCase(SysUser.class);
                    user.setUserName(entity.getStr("LOGIN_NAME"));
                    user.setNickName(entity.getStr("USER_NAME"));
                    return user;
                })
                .peek(user -> user.setPassword(SecurityUtils.encryptPassword(user.getUserName(), DEFAULT_PLAIN_PASSWORD)))
                .collect(Collectors.toList());
        sourceUserList.forEach(sysUserMapper::insertUser);
    }

    @SneakyThrows
    private void exec2() {
        String profile = SpringUtils.getActiveProfile();
        Db.use(DSFactory.get(profile))
                .find(Entity.create("sys_user").set("status", "0").set("del_flag", "0"))
                .stream()
                .peek(entity -> {
                    if ("CCSC".equalsIgnoreCase(entity.getStr("LOGIN_NAME"))) {
                        entity.set("USER_ID", 15L);
                    }
                })
                .map(entity -> {
                    SysUser user = entity.toBeanIgnoreCase(SysUser.class);
                    user.setUserName(entity.getStr("LOGIN_NAME"));
                    user.setNickName(entity.getStr("USER_NAME"));
                    String salt = entity.getStr("SALT");
                    String password = entity.getStr("PASSWORD");
                    user.setPassword(salt + password);
                    return user;
                })
                .forEach(sysUserMapper::insertUser);
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 1;
    }
}
